<?php

include_once(dirname(__FILE__).'/../services.database.php');

/*
    Las tablas a crear son:
      + fs_file_fsf (id_fsf,filename_fsf,mimetype_fsf,length_fsf,id_fsr)
      + fs_repository_fsr (id_fsr, name_fsr, path_fsr, parent_fsr=null)
      + VISTA: fs_file_info(id_fsf,filename_fsf,mimetype_fsf,length_fsf,id_fsr,path_ffi)
*/

class DBFileSystem extends DataService
{

    public function getInstance()
    {
        return(new DBFileSystem());
    }

    public function getRepository($id_fsr)
    {
        $sql = "SELECT *
                  FROM fs_repository_fsr
                 WHERE id_fsr = %i
               ";
        return $this->SafeGetRow($sql,$id_fsr);
    }
    
    public function insertRepository($name_fsr,$path_fsr,$parent_fsr=null)
    {
        $repository_info = array(
            'name_fsr'   => $name_fsr,
            'path_fsr'   => $path_fsr,
            'parent_fsr' => $parent_fsr // parent parece no ser necesario en la DB !! (porque los repositorios se guardan con el path completo de forma independiente.)
        );
        return $this->executeInsert('fs_repository_fsr',$repository_info,'id_fsr');
    }
    
    public function insertFile($filename_fsf,$mimetype_fsf,$length_fsf,$id_fsr)
    {
        $file_info = array(
            'filename_fsf' => $filename_fsf,
            'mimetype_fsf' => $mimetype_fsf,
            'length_fsf'   => $length_fsf,
            'id_fsr_fsf'   => $id_fsr
        );
        return $this->executeInsert('fs_file_fsf',$file_info,'id_fsf');
    }
    
    public function getFile($id_fsf)
    {
        $sql = "SELECT *
                  FROM fs_file_info
                 WHERE id_fsf = %i
               ";
        return $this->SafeGetRow($sql,$id_fsf);
    }
    
    public function removeFile($id_fsf)
    {        
        $sql = "DELETE 
                  FROM fs_file_fsf
                 WHERE id_fsf = %i
               ";
        
        $this->SafeExec($sql,$id_fsf);
        return true;
    }
    
    public function removeRepository($id_fsr)
    {
        $sql = "DELETE 
                  FROM fs_repository_fsr
                 WHERE id_fsr = %i
               ";
        $this->SafeExec($sql,$id_fsr);
        return true;
    }
    
    public function getRepositoryContent($id_fsr)
    {
        $sql = "SELECT *
                  FROM fs_file_info
                 WHERE id_fsr = %i
               ";
        return $this->SafeGetAll($sql,$id_fsr);
    }
    
    public function isWhitelisted($id_fsf)
    {
        $sql = "SELECT id_fsf
                  FROM fs_white_list
                 WHERE id_fsf = %i";
                 
        $result = $this->SafeGetOne($sql,$id_fsf);
        
        return $result!=null;
    }
}

?>